package org.jeecg.modules.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.flow.entity.ActHiProcessInstance;
import org.jeecg.modules.flow.mapper.ActHiProcessInstanceMapper;
import org.jeecg.modules.flow.query.HistoryInstanceQuery;
import org.jeecg.modules.flow.service.IActHiProcessInstanceService;
import org.jeecg.modules.flow.vo.HistoryProcessInstanceVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

import static org.jeecg.modules.flow.common.util.FlowUtils.beginOfDay;
import static org.jeecg.modules.flow.common.util.FlowUtils.endOfDay;

/**
 * @Description: actProcessInstance
 * @Author: jeecg-boot
 * @Date: 2021-12-29
 * @Version: V1.0
 */
@Service
public class ActHiHiProcessInstanceServiceImpl extends ServiceImpl<ActHiProcessInstanceMapper, ActHiProcessInstance> implements IActHiProcessInstanceService {

    @Resource
    private ActHiProcessInstanceMapper actHiProcessInstanceMapper;

    @Override
    public Page<HistoryProcessInstanceVo> queryHistoricProcessInstances(HistoryInstanceQuery query, Integer pageNo, Integer pageSize) {
        QueryWrapper<ActHiProcessInstance> queryWrapper = new QueryWrapper<>();
        Date startTimeBegin = beginOfDay(query.getStartTimeBegin());
        Date startTimeEnd = endOfDay(query.getStartTimeEnd());
        // ID_字段和PROC_INST_ID是一样的。
        queryWrapper.eq(StringUtils.isNotBlank(query.getProcessInstanceId()), "RES.id_", query.getProcessInstanceId())
                .eq(StringUtils.isNotBlank(query.getBusinessKey()), "RES.BUSINESS_KEY_", query.getBusinessKey())
                .like(StringUtils.isNotBlank(query.getName()), "RES.NAME_", query.getName())
                .gt(startTimeBegin != null, "RES.START_TIME_", startTimeBegin)
                .lt(startTimeEnd != null, "RES.START_TIME_", startTimeEnd)
                .orderByAsc("RES.START_TIME_");
        return actHiProcessInstanceMapper.queryHistoricProcessInstances(queryWrapper, new Page<>(pageNo, pageSize));
    }

}
